const http=require('http');
const url=require('url');
const querystring=require('querystring');

const domains=[
	'http://localhost:3000',
	'http://localhost:4000',
	'http://localhost:5000',
];


const server=http.createServer((req, res)=>{
	// 处理跨域 ==> origin==='null' || startsWith http://127.0.0.1:8848 
/* 	if(req.headers['origin']==='null' || req.headers['origin'].startsWith('http://127.0.0.1:8848')) {
		res.setHeader('access-control-allow-origin', '*');
	} */
	
	let origin=req.headers['origin'];
	console.log(origin);
	if(req.headers['origin']==='null') {
		res.setHeader('access-control-allow-origin', '*');
	} else {
		domains.forEach(item=>{
			if(origin.startsWith(item)) {
				res.setHeader('access-control-allow-origin', '*');
			}
		})
	}
	
	// 如果有多个域名==>怎么处理==>
	let {pathname, query}=url.parse(req.url, true);
	console.log(query);

	res.end('ok');
});

server.listen(3000, ()=>{
	console.log('server running success at http://localhost:3000');
});